<?php
if (!defined("__WD__")) exit("Wrong Connect");

class Model_BootstrapForm {

	private $o = null;

	function __construct() {
		
		$this->load = &getInstance( '__Load' );
		
		$this->load->package( "obj" );
		
		$this->clear();	
	}

	function __toString() {
		
		return $this->o->__toString();		
	}
	
	function getObj() {
		return $this->o;
	}

	function clear() {
		
		$this->o = new Obj("form");
		$this->o->attr("class", "form-horizontal");
	}

	function set($key, $value) {
		$this->o->attr($key, $value);
	}

	function inputHidden($name, $value) {
		$this->o->append(new Obj("input"))
			->last()->attr(array(
				'type' => "hidden",
				'name' => $name,
				'value' => $value
				
			));
	}
	function legend($context) {
		$this->o->append(new Obj("legend"))
			->last()->append($context);
	}
	function text($text, $label = null, $etc = array()) {
		$group = $this->o->append(new Obj("div"))->last()
			->addClass("control-group")
			->append(new Obj("label"))
			->append(new Obj("div"));
		$group->first()
			->addClass("control-label")
			->append($label);
		$group->last()
			->addClass("controls")
			->attr( $etc )
			->append($text);
	}
	function inputText($name, $value = null, $label = null, $etc = array()) {
		$group = $this->o->append(new Obj("div"))->last()
			->addClass("control-group")
			->append(new Obj("label"))
			->append(new Obj("div"));
		$group->first()
			->addClass("control-label")
			->attr("for", $name)
			->append($label);
		$group->last()
			->addClass("controls")
			->append(new Obj("input"))->last()
				->attr( array_merge(array('type' => "text", 'name' => $name, 'id' => $name, 'value'=>$value), $etc));
	}
	function inputPassword($name, $value = null, $label = null, $etc = array()) {
		$group = $this->o->append(new Obj("div"))->last()
			->addClass("control-group")
			->append(new Obj("label"))
			->append(new Obj("div"));
		$group->first()
			->addClass("control-label")
			->attr("for", $name)
			->append($label);
		$group->last()
			->addClass("controls")
			->append(new Obj("input"))->last()
				->attr( array_merge(array('type' => "password", 'name' => $name, 'id' => $name, 'value'=>$value), $etc));
	}
	function inputFile($name, $value = null, $label = null, $etc = array()) {
		$group = $this->o->append(new Obj("div"))->last()
			->addClass("control-group")
			->append(new Obj("label"))
			->append(new Obj("div"));
		$group->first()
			->addClass("control-label")
			->attr("for", $name)
			->append($label);
		$group->last()
			->addClass("controls")
			->append(new Obj("input"))->last()
				->attr( array_merge(array('type' => "file", 'name' => $name, 'id' => $name, 'value'=>$value), $etc));
	}
	function selectbox($name, $value = null, $label = null, $item=array(), $etc = array()) {
		$group = $this->o->append(new Obj("div"))->last()
			->addClass("control-group")
			->append(new Obj("label"))
			->append(new Obj("div"));
		$group->first()
			->addClass("control-label")
			->attr("for", $name)
			->append($label);
		$box = $group->last()
			->addClass("controls")
			->append(new Obj("select"))->last()
				->attr( array_merge(array('name' => $name, 'id' => $name), $etc));
		foreach($item as $key => $val) {
			if ($key == $value)
				$box -> append(new Obj("option"))->last()
					->attr("value", $key)->attr("selected", true)->append($val);
			else
				$box -> append(new Obj("option"))->last()
					->attr("value", $key)->append($val);
		}
	}
	function checkbox($name, $value = null, $label = null, $item=array(), $etc = array()) {
		if ( is_null($value) ) $value = array();
		$group = $this->o->append(new Obj("div"))->last()
			->addClass("control-group")
			->append(new Obj("label"))
			->append(new Obj("div"));
		$group->first()
			->addClass("control-label")
			->append($label);
		$box = $group->last()
			->addClass("controls");
		foreach($item as $key => $val) {
			if ( in_array($key, $value) )
				$box -> append(new Obj("label"))->last()
					->append(new Obj("input"))
					->append(" ".$val)
						->first()->attr(array("name" => $name, "value" => $key, "type" => "checkbox", "checked" => true));
			else
				$box -> append(new Obj("label"))->last()
					->append(new Obj("input"))
					->append(" ".$val)
						->first()->attr(array("name" => $name, "value" => $key, "type" => "checkbox"));
		}
	}
	function textarea($name, $value = null, $label = null, $etc = array()) {
		$group = $this->o->append(new Obj("div"))->last()
			->addClass("control-group")
			->append(new Obj("label"))
			->append(new Obj("div"));
		$group->first()
			->addClass("control-label")
			->attr("for", $name)
			->append($label);
		$group->last()
			->addClass("controls")
			->append(new Obj("textarea"))->last()
				->attr( array_merge(array('type' => "password", 'name' => $name, 'id' => $name), $etc))
				->append($value);
	}
	function submit($value, $label = null) {
		$group = $this->o->append(new Obj("div"))->last()
			->addClass("control-group")
			->append(new Obj("label"))
			->append(new Obj("div"));
		$group->first()
			->addClass("control-label")
			->attr("for", "submit")
			->append($label);
		$group->last()
			->addClass("controls")
			->append(new Obj("button"))->last()
				->attr( array('type' => "submit", 'name' => "submit", 'id' => "submit") )
				->addClass("btn btn-primary")
				->append($value);
	}
	function delete($value) {
		$this->o->last()->last()
			->append(" ")
			->append(new Obj("a"))->last()
				->attr( array('data-toggle' => "modal", 'href' => "#myDelete") )
				->addClass("btn btn-danger")
				->append($value);
	}
	function modal($title, $msg) {
		$group = $this->o->append(new Obj("div"))->last()
			->attr("id", "myDelete")
			->addClass("modal hide fade")
			->append(new Obj("div"))
			->append(new Obj("div"))
			->append(new Obj("div"));
		$header = $group->first()
			->addClass("modal-header")
			->append(new Obj("button"))
			->append(new Obj("h3"));
		$header->first()
			->attr(array("type" => "button", "data-dismiss" => "modal") )
			->addClass("close")
			->append("&times;");
		$header->last()
			->append($title);
		$group->first(1)
			->addClass("modal-body")
			->append(new Obj("p"))->last()->append($msg);
		$footer = $group->last()
			->addClass("modal-footer")
			->append(new Obj("a"))
			->append(new Obj("button"));
		$footer->first()
			->attr(array("href" => "#", "data-dismiss" => "modal") )
			->addClass("btn")
			->append("Close");
		$footer->last()
			->attr(array("type" => "submit") )
			->addClass("btn btn-danger")
			->append("Yes");
	}
	

// End Class
}

?>